package tests;

import common.Utils;
import org.junit.jupiter.api.Assertions;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;

/**
 * 管理员登录页面测试
 */
public class LoginPage extends Utils {

    private static String url = "http://82.156.185.199:8080/blogin.html";

    public LoginPage() {
        super(url);
    }

    /**
     * 检查页面是否可以正常打开
     */
    public void login() throws InterruptedException {
        // 左侧图片
        driver.findElement(By.cssSelector("body > div.login-box.row > div.img-box.col-sm-6.col-md-6.col-lg-7.col-xl-7 > img"));
        // 手机号
        driver.findElement(By.cssSelector("#loginForm > div:nth-child(1) > label:nth-child(1)"));
        // 手机号输入框
        driver.findElement(By.cssSelector("#phoneNumber"));
        // 密码
        driver.findElement(By.cssSelector("#loginForm > div:nth-child(2) > label:nth-child(1)"));
        // 密码输入框
        driver.findElement(By.cssSelector("#password"));
        // 登录按钮
        driver.findElement(By.cssSelector("#loginForm > button"));
        // 注册按钮
        driver.findElement(By.xpath("/html/body/div/div[2]/div[2]/a"));
        // 点击 注册按钮 是否跳转到注册页面
        driver.findElement(By.xpath("/html/body/div/div[2]/div[2]/a")).click();
        Thread.sleep(1000);
        String actualUrl = driver.getCurrentUrl();
        System.out.println("注册 actualUrl: " + actualUrl);
        String expectUel = "http://82.156.185.199:8080/register.html?admin=true";
        Assertions.assertEquals(expectUel, actualUrl);
        // 返回上一页
        driver.navigate().back();
    }

    /**
     * 登录失败
      */
    public void loginFail(){
        // 电话号码和密码都不填写
        driver.findElement(By.cssSelector("#loginForm > button")).click();
        // 检查 “请输入您的手机号” 是否存在
        driver.findElement(By.cssSelector("#phoneNumber-error"));
        // 检查 “请输入密码“ 是否存在
        driver.findElement(By.cssSelector("#password-error"));

        // 只填写电话号码
        // 刷新页面
        driver.navigate().refresh();
        driver.findElement(By.cssSelector("#phoneNumber")).sendKeys("15714393906");
        driver.findElement(By.cssSelector("#loginForm > button")).click();
        driver.findElement(By.cssSelector("#password-error"));

        // 只填写密码
        driver.navigate().refresh();
        driver.findElement(By.cssSelector("#password")).sendKeys("Fhb159357");
        driver.findElement(By.cssSelector("#loginForm > button")).click();
        driver.findElement(By.cssSelector("#phoneNumber-error"));

        // 填写正确的电话号码、错误的密码
        driver.navigate().refresh();
        driver.findElement(By.cssSelector("#phoneNumber")).sendKeys("15714393906");
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");
        driver.findElement(By.cssSelector("#loginForm > button")).click();
        // 处理弹窗
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert = driver.switchTo().alert();
        String actualText = alert.getText();
        System.out.println("弹窗文本：" + actualText);
        String expectText = "登录失败！密码错误！";
        Assertions.assertEquals(expectText, actualText);
        alert.accept();
    }

    /**
     * 登陆成功
     */
    public void loginSuc(){
        driver.navigate().refresh();
        driver.findElement(By.cssSelector("#phoneNumber")).sendKeys("15714393906");
        driver.findElement(By.cssSelector("#password")).sendKeys("Fhb159357");
        driver.findElement(By.cssSelector("#loginForm > button")).click();
        // 等页面跳转
        wait.until(ExpectedConditions.urlContains("admin.html"));
        // 登录成功后看是否跳转到 活动列表页
        String actUrl = driver.getCurrentUrl();
        System.out.println("活动列表页url: " + actUrl);
        String expectUrl = "http://82.156.185.199:8080/admin.html";
        Assertions.assertEquals(expectUrl, actUrl);
        // 校验 “活动管理”
        driver.findElement(By.cssSelector("body > div.cont-box > div.sidebar > ul > li:nth-child(1) > div > span"));
    }
}
